package com.qf.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qf.pojo.DtsOrder;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import com.qf.vo.GoodsRowsVo;
import com.qf.vo.OrderRowsVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 订单表 Mapper 接口
 * </p>
 *
 * @author zhaojian
 * @since 2021-05-12
 */
public interface DtsOrderMapper extends BaseMapper<DtsOrder> {


    /**
     * 按天统计订单的增长量和购买金额
     * @param daysAgo
     * @return
     */
    @Select("select DATE_FORMAT(t.add_time,\"%Y-%m-%d\") as dayStr, count(*) as cnts, sum(t.actual_price) as amts " +
            "from dts_order t " +
            "where t.deleted = 0 and t.add_time > DATE_ADD(NOW(),INTERVAL - #{daysAgo} day) and t.order_status not in(101, 102, 103) " +
            "group by  DATE_FORMAT(t.add_time,\"%Y-%m-%d\")")
    public List<DayStatis> statisIncrOrderCnt(@Param("daysAgo") int daysAgo);

    /**
     * 按照分类统计销售额
     * @return
     */
    @Select("select c.`name`, sum(o.actual_price) as value from dts_category c " +
            "left join dts_goods g on g.category_id = c.id " +
            "left join dts_order_goods og on og.goods_id = g.id " +
            "left join dts_order o on o.id = og.order_id " +
            "where og.deleted=0 and o.order_status not in(101, 102, 103) " +
            "group by c.`name`")
    public List<CategorySellAmts> categorySellStatis();

    @Select("select user_id userId  from dts_order where id = #{id}")
    public Integer selectUidByOrderId(@Param("id") Integer orderId);

    @Select("select sum(t.order_price) as amount,\n" +
            "count(*) as orders,\n" +
            "count(user_id) as customers ,\n" +
            "DATE_FORMAT(t.add_time,\"%Y-%m-%d\") as day,\n" +
            "sum(t.order_price)/count(user_id) as pcr \n" +
            "from dts_order t\n" +
            "where t.deleted = 0 and t.order_status not in(102,201,401,402)\n" +
            "group by  DATE_FORMAT(t.add_time,\"%Y-%m-%d\")")
    public List<OrderRowsVo> findAll();

    @Select("select sum(dts_order.goods_price) amount,count(dts_order.id) orders,date_format(dts_order_goods.add_time,'%Y-%m-%d') day,count(dts_order_goods.id) products\n" +
            "from dts_order\n" +
            "left join dts_order_goods on dts_order.id = dts_order_goods.order_id\n" +
            "where dts_order.deleted = 0 and dts_order.order_status not  in (101,102,103)\n" +
            "group by date_format(add_time,'%Y-%m-%d')")
    public List<GoodsRowsVo> queryAllGoodsStat();
}
